package com.qf.mybatisplus.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author code
 */
public class HttpClientUtil {

    private final static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    private static final RequestConfig REQUESTCONFIG = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(2000).setSocketTimeout(30000).build();


    public static String executeRequest(HttpUriRequest request, String username, String password) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            CredentialsProvider provider = new BasicCredentialsProvider();
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
                    username, password);
            provider.setCredentials(AuthScope.ANY, credentials);
            client = HttpClientBuilder.create()
                    .setDefaultCredentialsProvider(provider).build();

            response = client.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();

            // 判断返回状态是否为200
            if (statusCode == 200 || statusCode == 204) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                /**
                 * 2017-09-20 nimq
                 * 处理访问服务异常就抛出
                 */
                throw new RuntimeException(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return resultString;
    }

    public static byte[] executeRequestByte(HttpUriRequest request, String username, String password) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        byte[] bytes = null;
        try {
            CredentialsProvider provider = new BasicCredentialsProvider();
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
                    username, password);
            provider.setCredentials(AuthScope.ANY, credentials);
            client = HttpClientBuilder.create()
                    .setDefaultCredentialsProvider(provider).build();

            response = client.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();

            // 判断返回状态是否为200
            if (statusCode == 200 || statusCode == 204) {
                bytes = EntityUtils.toByteArray(response.getEntity());
            } else {
                /**
                 * 2017-09-20 nimq
                 * 处理访问服务异常就抛出
                 */
                throw new RuntimeException(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return bytes;
    }

    /**
     * Get提交方式
     *
     * @param url
     * @param param
     * @return
     */
    public static String doGet(String url, Map<String, String> param) {
        // 创建Httpclient对象
        String resultString = "";
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();

            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            httpGet.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            // 执行请求
            resultString = sendHttp(httpGet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return resultString;
    }

    /**
     * Get提交方式
     *
     * @param url
     * @return
     */
    public static String doGet(String url) {
        return doGet(url, null);
    }

    /**
     * 带验证头的Get提交方式
     *
     * @param url
     * @param username
     * @param password
     * @return
     */
    public static String doGetCredentials(String url, String username, String password) {
        // 创建Httpclient对象
        String resultString = "";
        HttpGet httpGet = null;
        try {
            // 创建http GET请求
            httpGet = new HttpGet(url);
            httpGet.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            resultString = executeRequest(httpGet, username, password);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
        return resultString;
    }

    /**
     * 发送表单 post请求
     *
     * @param httpUrl 地址
     * @param params  参数(格式:key1=value1&key2=value2)
     */
    public static String doPostUrlForm(String httpUrl, String params) {
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
        httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
        try {
            //设置参数
            StringEntity stringEntity = new StringEntity(params, "UTF-8");
            stringEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(stringEntity);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sendHttp(httpPost);
    }

    /**
     * Form表单提交
     *
     * @param url
     * @param param
     * @return
     */
    public static String doPostMapForm(String url, Map<String, String> param) {

        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            // 创建参数列表
            List<NameValuePair> paramList = new ArrayList<>();
            if (param != null) {
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
            }
            // 模拟表单
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
            httpPost.setEntity(entity);
            // 执行http请求
            resultString = sendHttp(httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return resultString;
    }

    /**
     * 无参数form post提交
     *
     * @param url
     * @return
     */
    public static String doPost(String url) {
        return doPostMapForm(url, null);
    }

    /**
     * 带验证头form表单提交
     *
     * @param url
     * @param param
     * @param username
     * @param password
     * @return
     */
    public static String doPostCredentials(String url, Map<String, String> param, String username, String password) {
        // 创建Httpclient对象
        HttpPost httpPost = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            httpPost = new HttpPost(url);
            httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            // 创建参数列表
            if (param != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : param.keySet()) {
                    paramList.add(new BasicNameValuePair(key, param.get(key)));
                }
                // 构造一个form表单式的实体
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
                //将请求实体设置到httpPost对象中
                httpPost.setEntity(entity);
            }
            // 执行http请求
            resultString = executeRequest(httpPost, username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);

        } finally {

        }

        return resultString;
    }

    /**
     * Json格式Post提交
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPostJson(String url, String json) {
        String resultString = "";
//        try {
        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));

        // 创建请求内容 构造一个form表单式的实体
        StringEntity entity = new StringEntity(json, Consts.UTF_8);
        //将请求实体设置到httpPost对象中
        httpPost.setEntity(entity);

        resultString = sendHttp(httpPost);
        return resultString;
    }


    /**
     * 封装httpclient的发送报文XML(text/xml)格式的请求的方法，携带请求头
     *
     * @param url
     * @param xmlString HG
     */
    public static String doPostXml(String url,String xmlString,Map<String,String> params){
        //创建httpclient工具对象
        HttpClient client = new HttpClient();
        //创建post请求方法
        PostMethod myPost = new PostMethod(url);
        //设置请求超时时间
        client.setConnectionTimeout(3000 * 1000);
        String responseString = null;
        try {
            //设置请求头部类型
            myPost.setRequestHeader("Content-Type", "text/xml");
            myPost.setRequestHeader("charset", "utf-8");
            if (params != null){
                for(Map.Entry<String, String> param:params.entrySet()){
                    myPost.setRequestHeader(param.getKey(),param.getValue());
                }
            }
            //设置请求体，即xml文本内容，一种是直接获取xml内容字符串，一种是读取xml文件以流的形式
            myPost.setRequestBody(xmlString);
            int statusCode = client.executeMethod(myPost);
            //只有请求成功200了，才做处理
            if (statusCode == org.apache.commons.httpclient.HttpStatus.SC_OK) {
                InputStream inputStream = myPost.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuffer stringBuffer = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    stringBuffer.append(str);
                }
                responseString = stringBuffer.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            myPost.releaseConnection();
        }
        return responseString;
    }

    /**
     * 封装httpclient的发送报文XML(text/xml)格式的请求的方法，携带鉴权参数，无鉴权参数传null
     * 原生调用，处理编码问题
     * @param url
     * @param xmlString HG
     */
    public static String doPostXmlHC(String url,String xmlString,Map<String,String> params){
        CloseableHttpClient client = null;
        CloseableHttpResponse resp = null;
        String resultMsg = "";
        try{
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
            if (params != null){
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    httpPost.addHeader(entry.getKey(), entry.getValue());
                }
            }
            client = HttpClients.createDefault();
            StringEntity entityParams = new StringEntity(xmlString,"utf-8");
            httpPost.setEntity(entityParams);
            client = HttpClients.createDefault();
            resp = client.execute(httpPost);
            resultMsg = EntityUtils.toString(resp.getEntity(),"utf-8");
            return resultMsg;
        }catch (Exception e){
        }finally {
            try {
                if(client!=null){
                    client.close();
                }
                if(resp != null){
                    resp.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultMsg;
    }

    /**
     * Json格式Post提交,指定ContentType
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPostJsonContentType(String url, String json, String contentType) {
        String resultString = "";
        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, contentType));
        httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
        // 创建请求内容 构造一个form表单式的实体
        StringEntity entity = new StringEntity(json, Consts.UTF_8);
        //将请求实体设置到httpPost对象中
        httpPost.setEntity(entity);
        resultString = sendHttp(httpPost);
        return resultString;
    }

    /**
     * Json格式Post提交,application/javascript
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPostJavascript(String url, String json) {
        String resultString = "";
        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/javascript"));
        httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));

        // 创建请求内容 构造一个form表单式的实体
        StringEntity entity = new StringEntity(json, Consts.UTF_8);
        //将请求实体设置到httpPost对象中
        httpPost.setEntity(entity);
        resultString = sendHttp(httpPost);
        return resultString;
    }

    /**
     * 带验证头json格式post提交
     *
     * @param url
     * @param json
     * @param username
     * @param password
     * @return
     */
    public static String doPostJsonCredentials(String url, String json, String username, String password) {
        // 创建Httpclient对象
        HttpPost httpPost = null;
        String resultString = "";
        if (json == null) {
            json = "{}";
        }
        try {
            // 创建Http Post请求
            httpPost = new HttpPost(url);
            httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
            httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            // 创建请求内容 构造一个form表单式的实体
            StringEntity entity = new StringEntity(json, Consts.UTF_8);
            //将请求实体设置到httpPost对象中
            httpPost.setEntity(entity);
            // 执行http请求
            resultString = executeRequest(httpPost, username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }

        return resultString;
    }


    /**
     * 带Token头的json格式post提交
     *
     * @param url   ;
     * @param json  ;
     * @param token ;
     * @return String
     */
    public static String doPostJsonToken(String url, String json, String token) {
        // 创建Httpclient对象
        HttpPost httpPost = null;
        String resultString = "";
        if (json == null) {
            json = "{}";
        }
        try {
            // 创建Http Post请求
            httpPost = new HttpPost(url);
            httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
            httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
            httpPost.addHeader(new BasicHeader(HttpHeaders.AUTHORIZATION, token));
            // 创建请求内容 构造一个form表单式的实体
            StringEntity entity = new StringEntity(json, Consts.UTF_8);
            //将请求实体设置到httpPost对象中
            httpPost.setEntity(entity);
            resultString = sendHttp(httpPost);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
        return resultString;
    }

    /**
     * 发送 post请求（带文件）
     */
    /*public static String doPostMutiPartForm(String httpUrl, Map<String, String> maps, List<File> fileLists) {
        HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
        MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create();
        for (String key : maps.keySet()) {
            meBuilder.addPart(key, new StringBody(maps.get(key), ContentType.TEXT_PLAIN));
        }
        for (File file : fileLists) {
            FileBody fileBody = new FileBody(file);
            meBuilder.addPart("files", fileBody);
        }
        HttpEntity reqEntity = meBuilder.build();
        httpPost.setEntity(reqEntity);
        return sendHttp(httpPost);
    }*/


//    /**
//     * 发送Post请求
//     *
//     * @param httpPost
//     * @return
//     */
//    private static String sendHttpPost(HttpPost httpPost) {
//        CloseableHttpClient httpClient = null;
//        CloseableHttpResponse response = null;
//        HttpEntity entity = null;
//        String responseContent = null;
//
//        try {
//
//            RequestConfig requestConfig = RequestConfig.custom()
//                    .setSocketTimeout(15000)
//                    .setConnectTimeout(15000)
//                    .setConnectionRequestTimeout(15000)
//                    .build();
//
//            // 创建默认的httpClient实例.
//            httpClient = HttpClients.createDefault();
//            httpPost.setConfig(requestConfig);
//            // 执行请求
//            response = httpClient.execute(httpPost);
//            entity = response.getEntity();
//            responseContent = EntityUtils.toString(entity, "UTF-8");
//            //responseContent = EntityUtils.toString(response.getEntity(), "utf-8");
//
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            try {
//                // 关闭连接,释放资源
//                if (response != null) {
//                    response.close();
//                }
//                if (httpClient != null) {
//                    httpClient.close();
//                }
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }
//        return responseContent;
//    }
    private static String sendHttp(HttpRequestBase httpRequestBase) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        String responseContent = null;
        try {
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(150000)
                    .setConnectTimeout(150000)
                    .setConnectionRequestTimeout(150000)
                    .build();

            // 创建默认的httpClient实例.
            httpClient = HttpClients.createDefault();
            httpRequestBase.setConfig(requestConfig);
            // 执行请求
            response = httpClient.execute(httpRequestBase);
            int statusCode = response.getStatusLine().getStatusCode();

            // 判断返回状态是否为200
            if (statusCode == 200 || statusCode == 204) {
                entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } else {
                /**
                 * 2017-09-20 nimq
                 * 处理访问服务异常就抛出
                 */
                throw new RuntimeException(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                // 关闭连接,释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return responseContent;
    }

    /**
     * 发送请求方法
     *
     * @param url
     * @param xmlstr
     * @return
     */
    public static String readContentFromPostBody(URL url, String xmlstr) {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setDoOutput(true);

            connection.setDoInput(true);

            connection.setUseCaches(false);

            connection.setInstanceFollowRedirects(true);

            connection.setRequestMethod("POST");//设置请求方式

            connection.setRequestProperty("Accept", "application/json");
            ; //设置接收数据的格式

            connection.setRequestProperty("Content-Type", "apploication/json");    //设置发送数据的格式
            connection.connect();

            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            out.append(xmlstr);

            out.flush();

            out.close(); // 读取响应

            StringBuilder sb = new StringBuilder();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line = null;
            line = reader.readLine();
            if (line != null) {
                sb = sb.append(line);
            }
            reader.close();
            connection.disconnect();
            logger.info("得到的sb為：{}", sb);
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            return jsonObject.getString("data");
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }

    public static String doPostHttpEntity(String serviceUrl, HttpEntity httpEntity) {


        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            HttpPost httpPost = new HttpPost(serviceUrl);
            httpPost.setConfig(REQUESTCONFIG);
            httpPost.setEntity(httpEntity);


            // 执行http请求
            response = httpClient.execute(httpPost);
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");


        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return resultString;
    }


//    public static void main(String[] args) {
//        String json = null;
//        String result = HttpClientUtil.doPostJsonCredentials("http://59.202.30.26:8082/GisqPlatformDesigner-Rest/service/bpf/definition/datumtypes/matchcodes"
//                , json, "admin", "admin");
//        String url= MessageFormat.format(
//                "http://59.202.30.26:8082/GisqPlatformDesigner-Rest/service/bpf/definition/datums?datumTypeGuid={0}&bizDefGuid={1}&range=0",
//        "273a98f2-a9ac-11e7-bd23-00163e0003d6","777f568e-a993-11e7-bd23-00163e0003d6");
//      String result=  HttpClientUtil.doGetCredentials(url,"admin","admin");
//      String result1=  HttpClientUtil.doGet(url);
//    }

    public static String doPost(String url, List<NameValuePair> params) {
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;

        HttpPost method = new HttpPost(url);
        method.setHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            client = HttpClients.createDefault();
            try {
                response = client.execute(method);
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity, "UTF-8");
                return data;
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            // 关闭连接,释放资源
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 添加头信息
     */
    public static String doPostHead(String url, List<NameValuePair> params, Map<String, String> map) {
        CloseableHttpResponse response = null;
        CloseableHttpClient client = null;

        HttpPost method = new HttpPost(url);
        method.setHeader("Content-Type", "application/x-www-form-urlencoded");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            method.setHeader(entry.getKey(), entry.getValue());
        }

        try {
            method.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            client = HttpClients.createDefault();
            try {
                response = client.execute(method);
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity, "UTF-8");
                return data;
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            // 关闭连接,释放资源
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Json格式Post提交
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPostJsonHead(String url, String json, Map<String, String> map) {
        String resultString = "";
//        try {
        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
        for (Map.Entry<String, String> entry : map.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }
        // 创建请求内容 构造一个form表单式的实体
        StringEntity entity = new StringEntity(json, Consts.UTF_8);
        //将请求实体设置到httpPost对象中
        httpPost.setEntity(entity);

        resultString = sendHttp(httpPost);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }

        return resultString;
    }
}

